package cn.tedu.vrd.mapper;

import cn.tedu.vrd.vo.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Insert("insert into product values(null,#{title},#{author},#{intro},#{url},0,0,#{created},#{categoryId})")
    void insert(Product product);

    //当查询的字段名和封装数据对象和属性名不一样时候必须单独指定
    @Select("select id,title,author,url,view_count,like_count from product")
    @Result(column = "view_count", property = "viewCount")
    @Result(column = "like_count", property = "likeCount")
    List<Product> select();

    @Delete("delete from product where id=#{id}")
    void delete(int id);

    @Select("select id,title,intro,url from product order by view_count desc limit 0,4")
    List<Product> selectView();

    @Select("select id,title,intro,url from product order by like_count desc limit 0,4")
    List<Product> selectLike();

    @Select("select * from product where id = #{id}")
    @Result(column = "view_count", property = "viewCount")
    @Result(column = "like_count", property = "likeCount")
    Product selectById(int id);

    @Update("update product set like_count=like_count+1 where id=#{id}")
    void like(int id);

    @Update("update product set view_count=view_count+1 where id=#{id}")
    void view(int id);

    @Select("select id,title,author,url,view_count,like_count from product where category_id=#{cid}")
    @Result(column = "view_count", property = "viewCount")
    @Result(column = "like_count", property = "likeCount")
    List<Product> selectByCid(int cid);

    //#{xxx}如果是字符串会自动添加单引号包裹
    //￥{xxx}不管是什么类型都不会加引号
    @Select("select id,title,author,url,view_count,like_count from product where title like '%${wd}%'")
    @Result(column = "view_count", property = "viewCount")
    @Result(column = "like_count", property = "likeCount")
    List<Product> selectByWd(String wd);

    @Select("select url from product where id=#{id}")
    String selectUrlById(int id);
}
